﻿using Payroll.Biz.Legacy.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Payroll.Biz.Legacy.Models
{
    public class CommissionClassification : IPaymentClassification
    {
        public decimal BaseRate { get; private set; }

        public decimal CommissionRate { get; private set; }

        public Dictionary<DateTime, SalesReceipt> salesReceipts { get; private set; }

        public CommissionClassification(decimal baseRate, decimal commissionRate)
        {
            this.BaseRate = baseRate;
            this.CommissionRate = commissionRate;
            this.salesReceipts = new Dictionary<DateTime, SalesReceipt>();
        }

        public void AddSalesReceipt(SalesReceipt receipt)
        {
            salesReceipts.Add(receipt.Date, receipt);
        }

        public SalesReceipt GetSalesReceipt(DateTime date)
        {
            return salesReceipts[date];
        }

        public decimal CalculatePay(Paycheck paycheck)
        {
            double salesTotal = 0;

#pragma warning disable S3267 // Loops should be simplified with "LINQ" expressions
            foreach (SalesReceipt receipt in salesReceipts.Values)
            {
                if (DateUtils.IsInPayPeriod(receipt.Date, paycheck.PayPeriodStartDate, paycheck.PayPeriodEndDate))
                {
                    salesTotal += receipt.SaleAmount;
                }
            }
#pragma warning restore S3267 // Loops should be simplified with "LINQ" expressions

            return BaseRate + (((decimal)salesTotal) * CommissionRate * ((decimal)0.01));
        }

        public override string ToString()
        {
            return $"{{{nameof(BaseRate)}={BaseRate.ToString()}, {nameof(CommissionRate)}={CommissionRate.ToString()}, {nameof(salesReceipts)}={salesReceipts}}}";
        }

        public override bool Equals(object obj)
        {
            return (obj is CommissionClassification classification) &&
                (BaseRate == classification.BaseRate) &&
                (CommissionRate == classification.CommissionRate);
        }

        public override int GetHashCode()
        {
            int hashCode = -1723941430;
            hashCode = (hashCode * (-1521134295)) + BaseRate.GetHashCode();
            hashCode = (hashCode * (-1521134295)) + CommissionRate.GetHashCode();
            return hashCode;
        }
    }
}
